package db;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
/**
 *
 * @author xDrake
 */
public class MySQL {

    private Statement stmt = null;
    private Connection con = null;
    private String driver = "";
    private String host = "";
    private String database = "";
    private String user = "";
    private String pass = "";

    public MySQL(String host, String database, String user, String pass) {

        this.driver = "com.mysql.jdbc.Driver";
        this.host = host;
        this.database = database;
        this.user = user;
        this.pass = pass;
    }

    public boolean Connect() {
        try {
            Class.forName(this.driver);
            if (this.con != null) {
                this.con.close();
            }
            this.con = DriverManager.getConnection("jdbc:mysql://" + host + ":3306/" + database, user, pass);

            if (this.stmt != null) {
                this.stmt.close();
            }
            this.stmt = (Statement) this.con.createStatement();

            if (this.con != null && this.stmt != null) {
                return true;
            } else {
                return false;
            }
        } catch (Exception ex) {
            return false;
        }
    }

    public boolean Close() {
        try {
            this.con.close();
            this.stmt.close();
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    public ResultSet querySelect(String sql, Object[] args) {
        try {
            PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                // обработка string
                if (args[i] instanceof String) {
                    ps.setString(i+1, String.valueOf(args[i]));
                }
                //обработка int
                if (args[i] instanceof Integer) {
                    ps.setInt(i+1, Integer.parseInt( args[i].toString()));
                }
                //обработка long
                if (args[i] instanceof Long) {
                    ps.setLong(i+1, Long.parseLong( args[i].toString()));
                }                  
            }            
            return ps.executeQuery();
        } catch (Exception ex) {
            return null;
        }

    }

    public int queryUpdate(String sql, Object[] args){
        try {
            PreparedStatement ps = (PreparedStatement) con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                // обработка string
                if (args[i] instanceof String) {
                    ps.setString(i+1, String.valueOf(args[i]));
                }
                //обработка int
                if (args[i] instanceof Integer) {
                    ps.setInt(i+1, Integer.parseInt( args[i].toString()));
                }
                //обработка long
                if (args[i] instanceof Long) {
                    ps.setLong(i+1, Long.parseLong( args[i].toString()));
                }                  
            }
            return ps.executeUpdate();
        } catch (Exception e) {
            return -1;
        }
    }
}
